package com.glq.book.servlet;

import com.glq.book.entity.Order;
import com.glq.book.service.OrderService;
import com.glq.book.vo.ShopVo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/order")
public class OrderServlet extends HttpServlet {
    private String type = "";
    private OrderService orderService = new OrderService();
    private Gson gson = new Gson();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        type = req.getParameter("type");
        if (type == null) {
            type = "findAll";
        }
        switch (type) {
            case "findAll":
                List<Order> orders = orderService.selectOrderList();
                req.getSession().setAttribute("orderList", orders);
                //跳转页面
                resp.sendRedirect("modeOrder.jsp");
                break;
            case "userOrderList":
                String username = req.getParameter("username");
                orders = orderService.selectOrderListByUser(username);
                req.getSession().setAttribute("orderList", orders);
                resp.sendRedirect("modeOrderList.jsp");
                //跳转页面
                break;
            case "orderInfo":
                String orderId = req.getParameter("orderId");
                Order order = orderService.selectByOrderId(orderId);
                req.getSession().setAttribute("order", order);
                //跳转页面
                break;
            case "shopCar":
                username = req.getParameter("username");
                List<ShopVo> shopCar = (List<ShopVo>) req.getSession().getAttribute(username + "shopCar");
                Gson gson = new Gson();
                resp.getWriter().write(gson.toJson(shopCar));
                break;
            default:
                //跳转错误页面
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        type = req.getParameter("type");
        if (type == null) {
            resp.sendRedirect("/order?type=findAll");
        }
        switch (type) {
            case "addShopCar":
                String username = req.getParameter("username");
                List<ShopVo> shopCar = (List<ShopVo>) req.getSession().getAttribute(username + "shopCar");
                if (shopCar == null) {
                    shopCar = new ArrayList<>();
                }
                String id = req.getParameter("id");
                String bookName = req.getParameter("bookName");
                String number = req.getParameter("number");
                String bookPrice = req.getParameter("bookPrice");
                String money = req.getParameter("money");
                ShopVo shopVo = new ShopVo(Integer.parseInt(id), bookName, Integer.parseInt(number), Integer.parseInt(bookPrice), Integer.parseInt(money));
                shopCar.add(shopVo);
                req.getSession().setAttribute(username + "shopCar", shopCar);
                resp.getWriter().write("添加购物车成功");
                break;
            case "addOrder":
                username = req.getParameter("username");
                String orderId = "LI-" + System.currentTimeMillis();
                List<ShopVo> listShop = gson.fromJson(req.getParameter("params"), new TypeToken<List<ShopVo>>() {
                }.getType());
                for (ShopVo vo : listShop) {
                    boolean b = orderService.insertOrder(vo.getBookName(), username, vo.getNumber(), vo.getMoney(), vo.getId(), orderId);
                    if (!b) {
                        resp.getWriter().write("fail");
                        break;
                    }
                }
                req.getSession().removeAttribute(username+"shopCar");
                resp.getWriter().write("结算成功");
//                bookName = req.getParameter("bookName");
//                username = req.getParameter("username");
//                number = req.getParameter("number");
//                money = req.getParameter("money");
//                String bookId = req.getParameter("bookId");
                //跳转页面
                break;
            default:
                //跳转错误页面
        }
    }
}
